#include "musicinfo.h"
#include "ui_musicinfo.h"

#include<QPainter>

MusicInfo::MusicInfo(bool islove,int index,QString title,QString singer,QString collection,QString duraion,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MusicInfo)
{
    ui->setupUi(this);
    m_index=index;
    m_title=title;
    m_singer=singer;
    m_collection=collection;
    m_duration=duraion;
    m_islove=islove;
    initinterface();

    setFixedHeight(50);

    initnetwork();
}

MusicInfo::MusicInfo(int index,QWidget *parent):
    QWidget(parent),
    ui(new Ui::MusicInfo)
{
    m_index=index;
}

MusicInfo::~MusicInfo()
{
    delete client;
    delete ui;
}

void MusicInfo::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter pa(this);
    if(m_index%2==0)
    {
       pa.fillRect(rect(), QColor(222,222,222));
    }
    else if(m_index==-1)
    {

    }
    else
    {
       pa.fillRect(rect(),QColor(255,255,255));
    }
}

void MusicInfo::mousePressEvent(QMouseEvent *event)
{
     Q_UNUSED(event);
}

void MusicInfo::mouseMoveEvent(QMouseEvent *event)
{
     Q_UNUSED(event);
}

void MusicInfo::initinterface()
{
    int width1=width()/20;
    ui->serialnumber->setFixedWidth(width1);
    ui->load->setFixedWidth(width1);
    ui->love->setFixedWidth(width1);

    ui->title->setFixedWidth(7*width1);
    ui->singer->setFixedWidth(3*width1);
    ui->collection->setFixedWidth(3*width1);
    ui->duration->setFixedWidth(3*width1);


    if(m_islove)
    {
        ui->love->setpicture(":/new/prefix1/res/love3.png",
                      ":/new/prefix1/res/love3.png",
                      ":/new/prefix1/res/love3.png");


    }
    else
    {
        ui->love->setpicture(":/new/prefix1/res/love1.png",
                      ":/new/prefix1/res/love1.png",
                      ":/new/prefix1/res/love1.png");

    }

    ui->load->setpicture(":/new/prefix1/res/xiazai(1).png",
                  ":/new/prefix1/res/xiazai2.png",
                  ":/new/prefix1/res/xiazai(1).png");


    ui->serialnumber->setText(QString::number(m_index));
    ui->title->setText(m_title);
    ui->singer->setText(m_singer);
    ui->duration->setText(m_duration);
    ui->collection->setText(m_collection);


}

void MusicInfo::initnetwork()
{
    client=new tcpclient();
}

void MusicInfo::on_love_clicked()   //添加到喜欢列表 移除喜欢列表
{
    m_islove=!m_islove;
    if(m_islove)
    {
        ui->love->setpicture(":/new/prefix1/res/love3.png",
                             ":/new/prefix1/res/love3.png",
                             ":/new/prefix1/res/love3.png");

        //添加到用户喜欢
        //添加喜欢音乐
//        usrnameid:{1373474316}\r\npassword:{137347}\r\nAddLoveMusic:{反方向的钟}\r\n
//        /删除喜欢音乐
//        usrnameid:{1373474316}\r\npassword:{137347}\r\nDelLoveMusic:{反方向的钟}\r\n
        emit addlove(m_title);

    }
    else
    {
        ui->love->setpicture(":/new/prefix1/res/love1.png",
                             ":/new/prefix1/res/love1.png",
                             ":/new/prefix1/res/love1.png");
       //取消喜欢
        emit dellove(m_title);
    }
}

void MusicInfo::mouseDoubleClickEvent(QMouseEvent *event)
{
    Q_UNUSED(event);
    // 在鼠标双击时触发信号
    emit doubleClicked(client,m_title);
}
